import 'dart:ui';

class HYSizeFit {
  static double physicalWidth;
  static double physicalHeight;
  static double screenWidth;
  static double screenHeight;
  static double dpr;
  static double statusHeight;

  static double rpx;
  static double px;

  static void initialize({double standardSize = 750}) {
    // 1.手机的物理分辨率
    physicalWidth = window.physicalSize.width;
    physicalHeight = window.physicalSize.height;
    print('分辨率:$physicalWidth * $physicalHeight');

    //2.手机屏幕的大小(逻辑分辨率) 放在这里会报错(context还没初始化完)
    // var width2 = MediaQuery.of(context).size.width;
    // var height2 = MediaQuery.of(context).size.height;

    //3.另一种获取屏幕的大小,获取dpr
    dpr = window.devicePixelRatio;
    screenWidth = physicalWidth / dpr;
    screenHeight = physicalHeight / dpr;
    print('屏幕宽高:$screenWidth * $screenHeight');

    //4.状态栏高度
    statusHeight = window.padding.top / dpr;
    print('状态栏高度:$statusHeight');

    //5.计算rpx大小
    rpx = screenWidth / standardSize;
    px = screenWidth / standardSize * 2;
  }

  static double setRpx(double size) {
    return rpx * size;
  }

  static double setPx(double size) {
    return rpx * size;
  }
}

extension IntFit on int {
  double get px {
    return HYSizeFit.setPx(this.toDouble());
  }

  double get rpx {
    return HYSizeFit.setRpx(this.toDouble());
  }
}

extension DoubleFit on double {
  double get px {
    return HYSizeFit.setPx(this);
  }

  double get rpx {
    return HYSizeFit.setRpx(this);
  }
}
